package com.salton123.base.feature

import android.Manifest
import android.annotation.SuppressLint
import android.os.Build
import com.blankj.utilcode.util.PermissionUtils
import com.qw.soul.permission.SoulPermission
import com.qw.soul.permission.bean.Permission
import com.qw.soul.permission.bean.Permissions
import com.qw.soul.permission.callbcak.CheckRequestPermissionListener
import com.qw.soul.permission.callbcak.CheckRequestPermissionsListener

/**
 * User: newSalton@outlook.com
 * Date: 2018/12/25 4:59 PM
 * ModifyTime: 4:59 PM
 * Description:
 */
@SuppressLint("ValidFragment")
class PermissionFeature(var callback: ((Boolean) -> Unit)? = null) : IFeature {

    private fun permissionArr(): MutableList<String> {
        val list = mutableListOf<String>()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            list.add(Manifest.permission.READ_MEDIA_IMAGES)
            list.add(Manifest.permission.READ_MEDIA_VIDEO)
            list.add(Manifest.permission.READ_MEDIA_AUDIO)
        } else {
            list.add(Manifest.permission.READ_EXTERNAL_STORAGE)
            list.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
        }
        list.add(Manifest.permission.CAMERA)
        return list
    }


    override fun onBind() {
        SoulPermission.getInstance().checkAndRequestPermissions(Permissions.build(*permissionArr().toTypedArray()), object : CheckRequestPermissionsListener {
            override fun onAllPermissionOk(allPermissions: Array<out Permission>?) {
                callback?.invoke(true)
            }

            override fun onPermissionDenied(refusedPermissions: Array<out Permission>?) {
                callback?.invoke(false)
            }

        })
    }


    override fun onUnBind() {

    }
}